﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.IO;
using System.Reflection;
using Opalis.QuickIntegrationKit;
using System.Management;

namespace Erlacher.Opalis.Virsto
{
    [OpalisObject("Get SnapShot", ShowFilters=false)]
    public class GetaSnapShot : IOpalisObject 
    {
        //private Settings settings;
        private string s_name = string.Empty;
        private string s_diskpath = string.Empty;
        private string s_description = string.Empty;
        private string s_server = string.Empty;
        private string s_username = string.Empty;
        private string s_password = string.Empty;

        [OpalisInput("Virsto Server")]
        public string Server
        {
            set { s_server = value; }
            get { return ""; }
        }

        [OpalisInput("Virsto Server - Username")]
        public string Username
        {
            set { s_username = value; }
            get { return ""; }
        }

        [OpalisInput("Virsto Server - Password", PasswordProtected = true)]
        public string Password
        {
            set { s_password = value; }
            get { return ""; }
        }


        [OpalisInput("SnapShot Name")]
        public string Name
        {
            set { s_name = value; }
            get { return ""; }
        }


        public void Design(IOpalisDesigner designer)
        {

            designer.AddOutput("SnapShotId");
            designer.AddOutput("ErrorState");
            designer.AddOutput("ErrorMessage");
            designer.AddOutput("Trace");
           
        }

        public void Execute(IOpalisRequest request, IOpalisResponse response)
        {
                string psscript = "";
                PowerShellHelper.ReturnObject Results;

                Assembly _assembly = Assembly.GetExecutingAssembly();
                StreamReader _textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("Erlacher.Opalis.Virsto.PSScripts.GetSnapShot.ps1"));
                psscript = _textStreamReader.ReadToEnd();

                psscript = psscript.Replace("%SNAPNAME%", this.s_name);
                psscript = psscript.Replace("%DISCRIPTION%", this.s_description);


                Results = PowerShellHelper.RunScript(psscript, s_username, s_password, s_server);

                Dictionary<string, object> publishedData = new Dictionary<string, object>();
                publishedData.Add("SnapShotId", Results.Value);
                publishedData.Add("ErrorState", Results.ErrorState.ToString());
                publishedData.Add("ErrorMessage", Results.ErrorMessage);
                publishedData.Add("Trace", Results.Trace);


                response.Publish(publishedData);

             
        }
    }
}